package cn.iocoder.yudao.module.system.controller.admin.officeinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.officeinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.officeinfo.OfficeinfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.officeinfo.SeatinfoDO;
import cn.iocoder.yudao.module.system.service.officeinfo.OfficeinfoService;

@Tag(name = "管理后台 - 办公室信息")
@RestController
@RequestMapping("/system/officeinfo")
@Validated
public class OfficeinfoController {

    @Resource
    private OfficeinfoService officeinfoService;

    @PostMapping("/create")
    @Operation(summary = "创建办公室信息")
    @PreAuthorize("@ss.hasPermission('system:officeinfo:create')")
    public CommonResult<Long> createOfficeinfo(@Valid @RequestBody OfficeinfoSaveReqVO createReqVO) {
        return success(officeinfoService.createOfficeinfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新办公室信息")
    @PreAuthorize("@ss.hasPermission('system:officeinfo:update')")
    public CommonResult<Boolean> updateOfficeinfo(@Valid @RequestBody OfficeinfoSaveReqVO updateReqVO) {
        officeinfoService.updateOfficeinfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除办公室信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:officeinfo:delete')")
    public CommonResult<Boolean> deleteOfficeinfo(@RequestParam("id") Long id) {
        officeinfoService.deleteOfficeinfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得办公室信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:officeinfo:query')")
    public CommonResult<OfficeinfoRespVO> getOfficeinfo(@RequestParam("id") Long id) {
        OfficeinfoDO officeinfo = officeinfoService.getOfficeinfo(id);
        return success(BeanUtils.toBean(officeinfo, OfficeinfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得办公室信息分页")
    @PreAuthorize("@ss.hasPermission('system:officeinfo:query')")
    public CommonResult<PageResult<OfficeinfoRespVO>> getOfficeinfoPage(@Valid OfficeinfoPageReqVO pageReqVO) {
        PageResult<OfficeinfoDO> pageResult = officeinfoService.getOfficeinfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OfficeinfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出办公室信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:officeinfo:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOfficeinfoExcel(@Valid OfficeinfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OfficeinfoDO> list = officeinfoService.getOfficeinfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "办公室信息.xls", "数据", OfficeinfoRespVO.class,
                        BeanUtils.toBean(list, OfficeinfoRespVO.class));
    }

    // ==================== 子表（办公室座位信息） ====================

    @GetMapping("/seatinfo/list-by-office-id")
    @Operation(summary = "获得办公室座位信息列表")
    @Parameter(name = "officeId", description = "办公室ID")
    @PreAuthorize("@ss.hasPermission('system:officeinfo:query')")
    public CommonResult<List<SeatinfoDO>> getSeatinfoListByOfficeId(@RequestParam("officeId") Long officeId) {
        return success(officeinfoService.getSeatinfoListByOfficeId(officeId));
    }

}